/*
 * HackSQL
 * Copyright (C) 2008-2009
 *
 * Authors: Felipe Nascimento Silva Pena
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * $Id: $
 */

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include "hs_execute.h"
#include "hs_parser_common.h"

#define NVL(x, y) (x ? x : y)

void hs_describe(int socket, hs_node *node)
{
	send(socket, node->val.data.str, strlen(node->val.data.str)+1, 0);
}

void hs_math_op(int socket, char op, hs_node *node1, hs_node *node2)
{
	char data[10];
	
	switch (op) {
		case '+':
			sprintf(data, "%d", node1->val.data.num + node2->val.data.num);
			break;
	}
	
	send(socket, data, strlen(data)+1, 0);
}

void hs_select_result(hs_parser_info *parser_info)
{
	hs_query *query = parser_info->query;
	int i;

	printf("SELECT {\n");
	printf("  ALL: %s\n", query->all ? "yes" : "no");
	printf("  DISTINCT: %s\n", query->distinct ? "yes" : "no");
	
	printf("  Fields (%d) {\n", query->num_select_itens);
	for (i = 0; i < query->num_select_itens; i++) {
		printf("    [name: '%s'; type: %d; alias: '%s']\n", (query->select_itens[i].val.type == IS_STRING ? query->select_itens[i].val.data.str : ""),
			query->select_itens[i].val.type, NVL(query->select_itens[i].alias, "none"));
	}
	printf("  }\n");
	
	printf("  Tables (%d) {\n", query->num_table_itens);
	printf("    DUAL: %s\n", query->dual ? "yes" : "no");
	for (i = 0; i < query->num_table_itens; i++) {
		printf("    [name: '%s'; alias: '%s']\n", query->table_itens[i].val.data.str, NVL(query->table_itens[i].alias, "none"));
	}
	printf("  }\n");	
	
	printf("}\n");
}
